﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ServerMonitor
{
    public partial class ReName : Form
    {
         Form1 f;
        public ReName(Form1 form)
         {
             f = form;
            InitializeComponent(); 
            BindLayout();
        }


        Label l = null;
        TextBox tb = null;
        int left = 10;
        int top = 10;
        private void BindLayout()
        { 
            List<Models.ProcessDescribe> temps = f.setting.Pds.Where(a => a.ProcessLavel != 1).OrderByDescending(a=>a.Memo).OrderBy(a=>a.ProcessName).ToList(); 
            for (int i = 0; i < temps.Count; i++)
            {
                
                    l = new Label();
                    l.Text = temps[i].ProcessName;
                    l.Left = left;
                    l.Top = top + i * 30;
                    l.Width = 80;
                    panel1.Controls.Add(l); 

                    tb = new TextBox();
                    tb.Name = temps[i].ProcessName+temps[i].ProcessPath;
                    tb.Text = temps[i].Memo;
                    tb.Left = left + 80;
                    tb.BorderStyle = BorderStyle.FixedSingle;
                    tb.Top = top-5 + i * 30;
                    tb.Width = 100;
                    panel1.Controls.Add(tb);





                    l = new Label();
                    l.Text = temps[i].ProcessPath; 
                    l.Left = left+180;
                    l.Top = top + i * 30;
                    l.Width =570;
                    panel1.Controls.Add(l); 


                 
             }
        }

        private void btSure_Click(object sender, EventArgs e)
        {
            string rName = string.Empty;
            bool close = true;
            for (int i = 0; i < f.setting.Pds.Count; i++)
            {
                if (f.setting.Pds[i].ProcessLavel == 0)
                {
                    try
                    {
                        rName = (panel1.Controls[f.setting.Pds[i].ProcessName + f.setting.Pds[i].ProcessPath] as TextBox).Text;
                        if (!f.setting.Pds.Any(a => a.Memo == rName && a.ProcessLavel == 1))
                        {
                            f.setting.Pds[i].Memo = rName;

                            //BindData();
                            Common.LogHelper.Log("重命名监控程序,名称：" + f.setting.Pds[i].ProcessName + "备注为："+rName+",路径：" + f.setting.Pds[i].ProcessPath);

                            close = true;
                        }
                        else
                        {
                            MessageBox.Show("不允许出现同主监控进程同名的进程，请修改");
                            close = false;
                        }
                    }
                    catch (Exception ex) { }
                }
            }
            if (close)
            {
                this.Close();
            } 
            this.DialogResult = DialogResult.OK;
        }

        private void btCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }
    }
}
